
/**
 * @desc 动态匹配正数(一位小数) 0.1 到 99999.9 max整数位置长度  array 其它额外值\
 */
export function validCustomADecimal(str, max = 5, array = []) {
  let customStr = ''
  array.forEach(v => {
    customStr += `|${v}`
  })
  const reg = new RegExp(`^((0\\.[1-9])|([1-9])|([1-9]\\d{0,${max - 1}})|([1-9]\\d{0,${max - 1}}\\.\\d{1})${customStr})$`)
  return reg.test(str)
}

/**
 * @desc 动态匹配正数(两位小数) 0.01 到 99999.99 max整数位置长度  array 其它额外值
 */
export function validCustomTwoDecimal(str, max = 5, array = []) {
  let customStr = ''
  array.forEach(v => {
    customStr += `|${v}`
  })
  const reg = new RegExp(`^(([1-9]\\d{0,${max - 1}})|(0\\.(0[1-9]|[1-9][0-9]{0,1}))|([1-9]\\d{0,${max - 1}}\\.\\d{1,2})${customStr})$`)
  return reg.test(str)
}

/**
 * @desc 动态匹配正整数数 1~99999 max整数位置长度0不限制
 */
export function validCustomPositiveInt(str, max = 5, array = []) {
  let customStr = ''
  let reg = ''
  array.forEach(v => {
    customStr += `|${v}`
  })
  if (max === 0) {
    reg = new RegExp(`^[1-9]\\d*${customStr}$`)
    return reg.test(str)
  } else {
    reg = new RegExp(`^(([1-9]\\d{0,${max - 1}})${customStr})$`)
    return reg.test(str)
  }
}

/**
 * @desc 获取字符串字符长度  传入str跟最大值length
 */
function getStrLength(str) {
  var realLength = 0; var len = str.length; var charCode = -1
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i)
    if (charCode >= 0 && charCode <= 128) realLength += 1
    else realLength += 2
  }
  return realLength
}
export function validStrLen(str, length) {
  const strLength = getStrLength(str)
  return strLength <= length
}

/**
 * @desc 匹配英文数字
 */
export function validEngNum(str, min = 1, max = 12) {
  const reg = new RegExp(`^[A-Za-z0-9]{${min},${max}}$`)
  return reg.test(str)
}
/**
 * @desc 匹配数字
 */
export function validNum(str, min = 1, max = 12) {
  const reg = new RegExp(`^[0-9]{${min},${max}}$`)
  return reg.test(str)
}

/**
 * @desc 匹配手机
 */
export function validMobilePhone(str) {
  const reg = /^1[0-9]{10}$/
  return reg.test(str)
}

/**
 * @desc 匹配Email
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @desc 匹配身份证
 */
export function validIDCard(str) {
  const reg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
  return reg.test(str)
}

/**
 * @desc 匹配WebUrl
 */
export function validWebUrl(str) {
  const reg = /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/
  return reg.test(str)
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
// export function validEmail(email) {
//   const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
//   return reg.test(email)
// }

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
